"""
    Controller for reservist table.
"""

from urllib import urlencode
from turbogears import redirect
from mnemoply import model
from mnemoply.mass_actions import MassAction, MassActionForm
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController
from mnemoply.mass_actions import MassAction
from mnemoply.widgets import DictionaryField

class CreateTemporaryPaymentRecord(MassAction):
    target_class = model.Reservist
    name = "create_temporary_payment_record"
    title = _("Create temporary payment record")

    form = MassActionForm(
        name=name,
        fields=[
            DictionaryField(
                name="trend_id",
                model_object_name='Trend',
                not_empty=True,
                label=_("Trend")),
            DictionaryField(
                name="organization_id",
                model_object_name='Organization',
                not_empty=True,
                label=_("Organization")),
            ])

    def call(self, objs, trend_id, organization_id):
        self.flash_message = None
        trend = model.Trend.get(int(trend_id))
        organization = model.Organization.get(int(organization_id))
        information_source = model.InformationSource.byStringId('unknown')

        for r in objs:
            if not r.temporaryPaymentRecord or not r.temporaryPaymentRecord.studentAgreement:
                r.temporaryPaymentRecord = model.TemporaryPaymentRecord(
                    organization=organization,
                    trend=trend,
                    informationSource=information_source,
                    tempSurname=r.surname,
                    tempForename=r.forename,
                    tempPatronymic=r.patronymic,
                    phone=r.phone,
                    register=None,
                    )
            else:
                self.flash_message = _("Not applied for all records.")

class ReservistController(LookupController, CRUDController):
    lookup_columns = [
        TableCol(name='surname', caption=_('Surname')),
        TableCol(name='forename', caption=_('Forename')),
        TableCol(name='patronymic', caption=_('Patronymic')),
        TableCol(name='office', caption=_('Interested office')),
        TableCol(name='plan', caption=_('Interested plan')),
        TableCol(name='trend', caption=_('Interested trend')),
        TableCol(name='comment', caption=_('Comment'), hidden=True),
        TableCol(name='possible_group', caption=_('Possible group')),
        TableCol(name='possible_group_start_date', caption=_('Possible group start date')),
        TableCol(name='phone', caption=_('Phone')),
        TableCol(name='email', caption=_('e-mail'), hidden=True),
        TableCol(name='wishes', caption=_('Wishes'), hidden=True),
        TableCol(name='customer_name', caption=_('Customer name'), hidden=True),
        TableCol(name='temporary_payment_record_id', caption=_('Temporary payment record id'), hidden=True),
        TableCol(name='is_agreement_created', caption=_('Is agreement created'), hidden=True),
        TableCol(name='date', caption=_('Creation date'), hidden=True),
        TableCol(name='last_phone_call_time', caption=_('Last phone call date'), hidden=True),
    ]
    model_object = model.Reservist
    edit_fields = [
        'surname', 'forename', 'patronymic',
        'phone', 'email',
        'interestedTrendID',
        'interestedOfficeID',
        'interestedPlanID',
        'wishes',

        'possibleGroupID',
        'comment',

        'lastPhoneCallTime',
        'customerName',
    ]
    view_fields = edit_fields + ['date']

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('reservist_lookup'),
            columns=self.lookup_columns,
            mass_actions_class=self.model_object,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = ReservistController()
